package org.example.managere.controller.feibiao;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.managere.dto.feibiao.AmAapMotorKnDto;
import org.example.managere.entity.feibiao.AmAapMotorKn;
import org.example.managere.service.feibiao.AmAapMotorKnService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import jakarta.validation.Valid;
import java.util.Map;

/**
 * 电机知识表控制器
 */
@RestController
@RequestMapping("/api/feibiao/aap/motorkn")
@RequiredArgsConstructor
@Slf4j
public class AmAapMotorKnController {
    
    private final AmAapMotorKnService service;
    
    /**
     * 分页查询
     */
    @GetMapping
    public ResponseEntity<Page<AmAapMotorKn>> findAll(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false, defaultValue = "timestamp,desc") String sort) {
        
        try {
            Sort sortObj;
            if (sort != null && !sort.isEmpty()) {
                String[] sortParams = sort.split(",");
                String sortField = sortParams[0];
                Sort.Direction direction = sortParams.length > 1 && sortParams[1].equalsIgnoreCase("asc") 
                    ? Sort.Direction.ASC : Sort.Direction.DESC;
                sortObj = Sort.by(direction, sortField);
            } else {
                sortObj = Sort.by(Sort.Direction.DESC, "timestamp");
            }
            
            Pageable pageable = PageRequest.of(page, size, sortObj);
            return ResponseEntity.ok(service.findAll(pageable));
        } catch (Exception e) {
            log.error("查询电机知识表失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public ResponseEntity<AmAapMotorKn> findById(@PathVariable Integer id) {
        try {
            return ResponseEntity.ok(service.findById(id));
        } catch (Exception e) {
            log.error("根据ID查询电机知识记录失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "记录不存在: " + e.getMessage());
        }
    }
    
    /**
     * 创建记录
     */
    @PostMapping
    public ResponseEntity<AmAapMotorKn> create(@Valid @RequestBody AmAapMotorKnDto dto) {
        try {
            return ResponseEntity.status(HttpStatus.CREATED).body(service.create(dto));
        } catch (Exception e) {
            log.error("创建电机知识记录失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新记录
     */
    @PutMapping("/{id}")
    public ResponseEntity<AmAapMotorKn> update(@PathVariable Integer id, @Valid @RequestBody AmAapMotorKnDto dto) {
        try {
            return ResponseEntity.ok(service.update(id, dto));
        } catch (Exception e) {
            log.error("更新电机知识记录失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除记录
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        try {
            service.delete(id);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            log.error("删除电机知识记录失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 全文搜索混合文本
     */
    @GetMapping("/search/hybrid")
    public ResponseEntity<Page<AmAapMotorKn>> searchByHybridData(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        try {
            Pageable pageable = PageRequest.of(page, size);
            return ResponseEntity.ok(service.searchByHybridData(keyword, pageable));
        } catch (Exception e) {
            log.error("搜索混合文本失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "搜索失败: " + e.getMessage());
        }
    }
    
    /**
     * 全文搜索分析结果
     */
    @GetMapping("/search/analysis")
    public ResponseEntity<Page<AmAapMotorKn>> searchByLlmAnalysis(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        try {
            Pageable pageable = PageRequest.of(page, size);
            return ResponseEntity.ok(service.searchByLlmAnalysis(keyword, pageable));
        } catch (Exception e) {
            log.error("搜索分析结果失败: {}", e.getMessage(), e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "搜索失败: " + e.getMessage());
        }
    }
} 